import numpy as np
import csv
import matplotlib.pyplot as plt

logPath = 'D:/VR_project/ViewPrediction/online/log/90x160/'   # 90x160
# logPath = 'D:/VR_project/ViewPrediction/online/log/5x5/'   # 5x5

fileList = ['Alien_3.csv', 'Conan1_3.csv', 'Conan2_3.csv', 'Cooking_3.csv',
             'Rhinos_3.csv', 'Skiing_3.csv',  'Surfing_3.csv', 'War_3.csv']

nameList = []
frameAccList = []
tileAccList = []
recallList = []
precisionList = []

for f in fileList:
    nameList.append(f.split('_')[0])

    with open(logPath + f, 'r') as csvfile:
        reader = csv.reader(csvfile)
        rows = [row for row in reader]

        frameAccList.append(round(float(rows[-4][1]), 4))
        tileAccList.append(round(float(rows[-3][1]), 4))
        recallList.append(round(float(rows[-2][1]), 4))
        precisionList.append(round(float(rows[-1][1]), 4))

# TileMetrics
x = np.arange(len(nameList))
width = 0.2
plt.rcParams['font.size'] = 20
fig, ax = plt.subplots(figsize=(20, 8))
n = 0

plt.bar(x + width * n, tileAccList, width=width, label='AvgTileAccuracy')
plt.bar(x + width * (n+1), recallList, width=width, label='AvgRecall')
plt.bar(x + width * (n+2), precisionList, width=width, label='AvgPrecision')

plt.grid(linestyle='--')
plt.xticks(x + width, nameList, fontsize=20)
plt.ylim(0, 1)
ax.set_ylabel('AverageMetrics')
ax.yaxis.set_major_locator(plt.MultipleLocator(0.1))

plt.legend(loc='center', ncol=4, bbox_to_anchor=[0.5, 0.95])
# plt.title('5 x 5')
# plt.title('90 x 160')
plt.show()


# # FrameAccuracy 柱状图
# x = np.arange(len(nameList))
# width = 0.5
# fig, ax = plt.subplots()
#
# plt.bar(x, frameAccList, width=width, label='FrameAccuracy')
# plt.grid(linestyle='--')
# plt.xticks(x, nameList)
# plt.ylim(0, 1)
# ax.set_ylabel('PredictionAccurancy')
# ax.yaxis.set_major_locator(plt.MultipleLocator(0.1))
# plt.show()


print('matplotlib plot finish!')